package com.sugon.modules.excel.impl;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.sugon.dao.ToolsBaseDao;
import com.sugon.dao.ToolsCheckJudgeStandardDao;
import com.sugon.entity.ToolsBaseEntity;
import com.sugon.entity.ToolsCheckJudgeStandardEntity;
import com.sugon.modules.excel.IExcelHandler;
import com.sugon.modules.excel.dto.ToolsCheckStandardImportParamDTO;
import com.sugon.service.ToolsCheckJudgeStandardService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 工装周期性验证判定标准Excel导入Handler
 *
 * @author YuXD
 * @since 2021-09-23
 */
@Service("toolsCheckJudgeStandardExcelHandler")
public class ToolsCheckJudgeStandardExcelHandler implements IExcelHandler {

    @Resource
    private ToolsBaseDao toolsBaseDao;
    @Resource
    private ToolsCheckJudgeStandardService toolsCheckJudgeStandardService;
    @Resource
    private ToolsCheckJudgeStandardDao toolsCheckJudgeStandardDao;

    /**
     * @param excelInputStream excel输入流
     * @return
     * @throws Exception
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int importExcelFromStream(InputStream excelInputStream) throws Exception {
        // 获取导入配置
        ImportParams importParams = getImportParams();
        // 从上传文件中读取叉车台账列表
        List<ToolsCheckStandardImportParamDTO> forkTruckAccountList = ExcelImportUtil.importExcel(excelInputStream, ToolsCheckStandardImportParamDTO.class, importParams);
        if (CollectionUtil.isEmpty(forkTruckAccountList)) {
            return 0;
        }
        int importRows = 0;

        ToolsCheckJudgeStandardEntity checkStandard;
        String mapNo;
        // 工装档案
        ToolsBaseEntity toolsFile;
        // 暂存map，防止出现多次查询
        Map<String, ToolsBaseEntity> tem = new HashMap<>();
        CopyOptions copyOptions = CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true);
        for (ToolsCheckStandardImportParamDTO param : forkTruckAccountList) {
            // 图号必填，如果为空，不做处理继续下一条
            mapNo = param.getMapNo();
            if (StrUtil.isEmpty(mapNo)) {
                continue;
            }
            // 获取工装基础档案信息
            toolsFile = tem.get(mapNo);
            if (toolsFile == null) {
                toolsFile = toolsBaseDao.loadByMapNo(mapNo);
                if (toolsFile == null) {
                    continue;
                }
                tem.put(mapNo, toolsFile);
            }
            // 简单校验下一个工装同一个检测标准只允许存在一个
            Assert.notBlank(param.getJudgeStandard());
            List<String> query = toolsCheckJudgeStandardDao.query(toolsFile.getId(), param.getJudgeStandard());
            if (CollectionUtil.isNotEmpty(query)) {
                continue;
            }
            // 设置
            checkStandard = new ToolsCheckJudgeStandardEntity();
            BeanUtil.copyProperties(param, checkStandard, copyOptions);
            checkStandard.setToolsFileId(toolsFile.getId());
            // 处理下校验周期
            String checkPeriodDesc = param.getCheckPeriodDesc();
            Integer checkPeriod = 1;
            if ("1次/月".equals(checkPeriodDesc)) {
                checkPeriod = 1;
            } else if ("1次/季度".equals(checkPeriodDesc)) {
                checkPeriod = 3;
            } else if ("1次/半年".equals(checkPeriodDesc)) {
                checkPeriod = 6;
            } else if ("1次/年".equals(checkPeriodDesc)) {
                checkPeriod = 12;
            }
            checkStandard.setCheckPeriod(checkPeriod);
            importRows += toolsCheckJudgeStandardService.save(checkStandard);
        }
        return importRows;
    }

    /**
     * 设置导入参数
     *
     * @return
     */
    @Override
    public ImportParams getImportParams() {
        ImportParams params = new ImportParams();
        params.setHeadRows(1);
        params.setKeyIndex(0);
        return params;
    }

}
